|
|||
![]() A tiny basic event driven xml parser written in C with a C++ wrapper class. Provides an easy way to parse xml in C and C++ programs. |
|||
1. Features
2. Downloads
3. HowTo
copy the file lt_parser.c into your source folder copy the file lt_wrapper.h into your header folder copy the file lt_parser.c into your source folder No specific compiler/linker options are required.
4. Code sample
/** * demo.c **/ #include "lt_parser.h" #include <stdio.h> int OnBegin(LX_PARSER *lx){ return 0; } int OnFinnish(LX_PARSER *lx){ return 0; } int OnTag(LX_PARSER *lx, unsigned char *tagname, unsigned char *content){ printf("<%s>\n", tagname); printf("\t%s\n", content); return 0; } int OnTagEnd(LX_PARSER *lx, unsigned char *tagname){ return 0; } int OnAttribute(LX_PARSER *lx, unsigned char *tagname, unsigned char *attname, unsigned char *attvalue){ return 0; } void OnError(LX_PARSER *lx, size_t line, char *error){ printf("Error at %ld: %s\n", line, error); } int main(void){ LX_PARSER *lx = lt_new(); printf("Lepton XML ansi c demo\n"); lx->OnAttribute = OnAttribute; lx->OnBegin = OnBegin; lx->OnError = OnError; lx->OnFinnish = OnFinnish; lx->OnTag = OnTag; lx->OnTagEnd = OnTagEnd; if(lt_loadfile(lx, "c:\\test.xml")){ lt_parse(lx); } lx = lt_delete(lx); return 0; } /** * demo.cpp **/ #include "lt_wrapper.hpp" #include <iostream.h> class MyParser : protected LTPARSER{ private: char *mPath; public: MyParser(){ this->mPath = ""; } ~MyParser(){ } void doParse(char *path){ this->mPath = path; if( this->loadfile(this->mPath) ) this->parse(); } int OnBegin(LTPARSER *lxp){ cout << "START " << this->mPath << cout << std::endl; return 0; } int OnFinnish(LTPARSER *lxp){ cout << "DONE " << this->mPath << std::endl; return 0; } int OnTag(LTPARSER *lxp, unsigned char *tagname, unsigned char *content){ cout << "\t" << tagname << std::endl; cout << "\t\t" << content << std::endl; return 0; } int OnTagEnd(LTPARSER *lxp, unsigned char *tagname){ return 0; } int OnAttribute(LTPARSER *lx, unsigned char *tagname, unsigned char *attname, unsigned char *attvalue){ cout << "\t\t\t" << attname << " = " << attvalue << std::endl; return 0; } void OnError(LTPARSER *lxp, size_t line, char *error){ cerr << error << std::endl; } }; int main(void){ MyParser mp; cout << "Lepton XML C++ demo" << std::endl; mp.doParse("./test.xml"); return 0; } |
|||
![]() |
|||
|